package com.ledict.casefiling.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.PropertySource;

import javax.sql.DataSource;
import java.sql.SQLException;

@Configuration
@PropertySource(value = "classpath:jdbc.properties")
public class DruidDataSourceConfig {
    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @Value("${spring.datasource.url}")
    private String jdbcUrl;

    @Value("${spring.datasource.driver-class-name}")
    private String driverClass;

    @Value("${spring.datasource.initialSize}")
    private int initialSize;

    @Value("${spring.datasource.maxActive}")
    private int maxActive;

    @Value("${spring.datasource.minIdle}")
    private int minIdle;

    @Value("${spring.datasource.maxWait}")
    private long maxWait;

    @Value("${spring.datasource.druid.filters}")
    private String filters;

    @Value("${druid.web.allow}")
    private String webAllow;
    @Value("${druid.web.deny}")
    private String webDeny;
    @Value("${druid.web.login.username}")
    private String webUsername;
    @Value("${druid.web.login.passwd}")
    private String webPasswd;
    @Value("${druid.web.reset.enable}")
    private String resetEnable;

    @Bean(name = "primaryDataSource")
    @Primary
    public DataSource dataSource() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUsername(username);
        dataSource.setPassword(password);
        dataSource.setUrl(jdbcUrl);
        dataSource.setDriverClassName(driverClass);
        dataSource.setMaxActive(maxActive);
        dataSource.setMaxWait(maxWait);
        dataSource.setFilters(filters);

        return dataSource;
    }

    // 如果要想进行 Druid 的性能的监控操作，则需要做一些基础配置，例如：你访问的 IP 地址是否是白名单。

    // 主要实现web监控的配置处理
    @Bean
    public ServletRegistrationBean druidServlet() {
        // 现在要进行druid监控的配置处理操作
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*");

        // 白名单
        registrationBean.addInitParameter("allow", webAllow);
        // 黑名单
        registrationBean.addInitParameter("deny", webDeny);
        registrationBean.addInitParameter("loginUserName", webUsername);
        registrationBean.addInitParameter("loginPasswd", webPasswd);
        registrationBean.addInitParameter("resetEnable", resetEnable);

        return registrationBean;
    }

    @Bean
    public FilterRegistrationBean filterRegistrationBean() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();

        filterRegistrationBean.setFilter(new WebStatFilter());
        filterRegistrationBean.addUrlPatterns("/*"); // 所有请求进行监控处理
        filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.css,/druid/*");

        return filterRegistrationBean;
    }
}
